<?php

if(!defined("INDEX"))
    die;

if($user->id == 0) {
    header('Location: /login');
    exit;
}

// ---------------------------------------------------------------------------

try {

    if(isset($_GET['release_id'])) {
        
        $smarty->assign('op', 'watch');
        
        $release = Release::getById($_GET['release_id']);
        $smarty->assign('release', $release);
        
        $user->watchRelease($_GET['release_id']);
        
    } elseif(isset($_GET['remove_release_id'])) {
        
        $smarty->assign('op', 'remove');
        
        $release = Release::getById($_GET['remove_release_id']);
        $smarty->assign('release', $release);
        
        $user->unwatchRelease($_GET['remove_release_id']);

    } elseif(isset($_GET['remove_artist_id'])) {

        $smarty->assign('op', 'remove_artist');

        $artist = Artist::getById($_GET['remove_artist_id']);
        $smarty->assign('artist', $artist);

        $user->unwatchArtist($_GET['remove_artist_id']);
    }

    $view = obtainHTTPVar('view', null);
    if(!in_array($view, array('releases', 'artists')))
        $view = 'releases';

    $filter = obtainHTTPVar('filter', null);

    $smarty->assign('view', $view);
    $smarty->assign('filter', $filter);

    switch($view) {

        case 'releases':
            $smarty->assign('releases', $user->getWatchedReleases($filter));
            break;

        case 'artists':
            $smarty->assign('artists', $user->getWatchedArtists($filter));
            break;

    }
    
    

} catch (Exception $e) {

    switch($e->getMessage()) {
        case _('Failed to load release.'):
            throw new Exception(_('Sorry, no such release.'));
            break;
        default:
            throw new Exception($e->getMessage());
            break;
    }
}